﻿using AntdUI;
using PPSMS.Commons;
using PPSMS.Controllers;
using PPSMS_BLL;
using PPSMS_Model;
using Sunny.UI;
namespace PPSMS
{
    public partial class InverterStatusForm : UIPage
    {
        private InverterDevices_BLL _inverterDevice_BLL = new();
        private InverterStatistics_BLL _inverterStatistics = new();
        private InverterStatus_BLL _inverterStatus_BLL = new();

        AntList<InverterStatusRecord> inverterStatusRecords = new();
        public static event Action OnChangePage;
        private static long id;

        public InverterStatusForm()
        {
            InitializeComponent();
            PSStatusForm.OnInverterClick += Refresh;
        }


        private void select1_SelectedIndexChanged_1(object sender, IntEventArgs e)
        {
            var inverterId = Convert.ToInt32(select1.Text);
            var inverterDevices = _inverterDevice_BLL.GetDeviceById(inverterId);
            Refresh(inverterDevices);
        }
        private void Refresh(InverterDevices inverterDevice)
        {
            Lbl_InverterID.Text = inverterDevice.InverterID.ToString();
            Lbl_manuf.Text = inverterDevice.Manufacturer;
            Lbl_RatedPower.Text = inverterDevice.RatedPower;
            var inverterStatus = _inverterStatus_BLL.SelectById(inverterDevice.InverterID, "");
            var inverterStatistics = _inverterStatistics.SelectByIdDate(inverterDevice.InverterID, DateTime.Now.ToString("yyyy-MM-dd"));
            if (inverterStatistics == null || inverterStatus == null)
            {
                inverterStatus = ReadOPCToList.ReadInverterStatusFromOPC((int)inverterDevice.InverterID, inverterDevice);
                inverterStatistics = ReadOPCToList.ReadInverterStatisticsFromOPC((int)inverterDevice.InverterID, inverterDevice);
            }

            Lbl_DMPG.Text = inverterStatus.CurrentPower.ToString();//日最大发电功率
            //日发电量
            var cumulativeEnergy = inverterStatistics?.DailyEnergy ?? 1.1;
            Lbl_DEG.Text = cumulativeEnergy.ToString("F1");
            Lbl_MEG.Text = (cumulativeEnergy * 30).ToString("F1");//月发电量
            Lbl_YEG.Text = (cumulativeEnergy * 360).ToString("F1");//年发电量

            Lbl_DI.Text = (cumulativeEnergy * 0.67).ToString("F1");//日收益
            Lbl_MI.Text = (cumulativeEnergy * 0.67 * 30).ToString("F1");//月收益
            Lbl_YI.Text = (cumulativeEnergy * 0.67 * 360).ToString("F1");//年收益

            id = inverterDevice.InverterID;

            trendsControl1.ID = (int?)inverterDevice.InverterID;
            OnChangePage?.Invoke();

            inverterStatusRecords.Clear();
            var inverterStatusRecord = new InverterStatusRecord(
                //table2
                (double)inverterStatus.ArrayVoltage,
                (double)inverterStatus.ArrayCurrent,
                (double)inverterStatus.GridVoltage,
                (double)inverterStatus.CurrentA,
                (double)inverterStatus.CurrentB,
                (double)inverterStatus.CurrentC,
                //table1
                (double)inverterStatus.InternalTemperature,
                (double)inverterStatus.Frequency,
                (double)inverterStatus.CurrentPower,
                (double)inverterStatistics.DailyEnergy,
                (double)inverterStatistics.CO2Reduction
            );
            inverterStatusRecords.Add(inverterStatusRecord);
            table1.Binding(inverterStatusRecords);
            table2.Binding(inverterStatusRecords);
        }

        private void InverterStatusForm_Load(object sender, EventArgs e)
        {

            List<InverterDevices> inverterDevices = _inverterDevice_BLL.SelectAllDevices();
            foreach (var inverter in inverterDevices)
            {
                select1.Items.Add(inverter.InverterID);
            }
            table1.Columns = new ColumnCollection(new List<Column>()
            {
                new("Temperature","机内温度/℃"),
                new("Frequency","频率/Hz"),
                new("AveragePower","平均发电功率/kW"),
                new("CumulativeEnergy","累计发电量/kW·h"),
                new("CO2Reduction","累计二氧化碳排放/t"),
            });


            table2.Columns = new ColumnCollection(new List<Column>()
            {
                new("ArrayVoltage","阵列电压/V"),
                new("ArrayCurrent","阵列电流/A"),
                new("GridVoltage","并网电压/V"),
                new("Current_A","交流测电流IA/A"),
                new("Current_B","交流测电流IB/A"),
                new("Current_C","交流测电流IC/A"),
            });

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            dashBoard2.ActualValue = (float)Extension.ReadOPCValue($"Channel1.逆变器{id}.currentPowerGeneration");
        }
    }
    public record InverterStatusRecord
    (
        //table2
        double ArrayVoltage,
        double ArrayCurrent,
        double GridVoltage,
        double Current_A,
        double Current_B,
        double Current_C,
        //table1
        double Temperature,
        double Frequency,
        double AveragePower,
        double CumulativeEnergy,
        double CO2Reduction
    );
}